<template>
  <div>
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs>
          <el-tab-pane label="角色管理" />
          <el-tab-pane label="公司信息" />
        </el-tabs>

        <el-row>
          <el-button type="primary" size="small" @click="addRoles">+新增角色</el-button>
        </el-row>

        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="角色名称" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="doEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="doDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-row>
          <el-pagination
            background
            :current-page="params.page"
            :page-size="params. pagesize"
            layout="prev, pager, next"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加' "
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="flag"
      >
        <el-form label-width="100px" :model="getRole" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="getRole.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="getRole.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="offWid">取消</el-button>
            <el-button size="small" type="primary" @click="doSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配角色"
        :visible.sync="showDialogAssign"
      >
        <assign-permission v-if="showDialogAssign" :cur-id="CurId" @cancel="cancel" />
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getRoles, addRoles, delRoles, editRoles } from '@/api/settings'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 2
      },
      flag: false,
      total: 0,
      getRole: {
        name: '',
        description: ''
      },
      isEdit: false,
      CurId: '',
      showDialogAssign: false,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      const res = await getRoles(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    pageChange(curPage) {
      this.params.page = curPage
      this.loadRoles()
    },
    // 编辑弹窗
    doEdit(row) {
      this.isEdit = true
      this.getRole = { ...row }
      this.flag = true
    },
    // 添加弹窗
    addRoles() {
      this.getRole = {
        name: '',
        description: ''
      }
      this.isEdit = false
      this.flag = true
    },
    // 关闭弹窗
    offWid() {
      this.flag = false
      this.getRole = {
        name: '',
        description: ''
      }
    },
    // 提交(确认添加 | 确认编辑)
    async doSubmit() {
      if (this.isEdit) {
        await editRoles(this.getRole)
        this.$message.success('修改成功')
        this.flag = false
        this.loadRoles()
      } else {
        await addRoles(this.getRole)
        this.flag = false
        this.$message.success('添加成功')
        this.total++
        this.params.page = Math.ceil(this.total / this.params.pagesize)
        this.loadRoles()
      }
    },
    // 调用删除方法
    async loadDel(id) {
      await delRoles(id)
      this.loadRoles()
      if (this.list.length === 1) {
        this.params.page--
        if (this.params.page === 0) {
          this.params.page = 1
        }
        this.loadRoles()
      }
    },
    // 删除角色
    doDel(id) {
      this.$confirm('你确定要删除此部门吗?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loadDel(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分配权限弹出窗口
    hAssign(id) {
      this.showDialogAssign = true
      this.CurId = id
    },
    // 子组件关闭弹窗
    cancel() {
      this.showDialogAssign = false
      this.loadRoles()
    }
  }
}
</script>
